local sk__shenfu = fk.CreateSkill {

  name = "sk__shenfu",

  tags = {},

}



sk__shenfu:addEffect(fk.AfterCardsMove, {
  name = "sk__shenfu",
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__shenfu.name) and player:getHandcardNum() < 4 then
      for _, move in ipairs(data) do
        if move.from == player then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand then
              return true
            end
          end
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local mark = player:getTableMark("@sk__shenfu")
    for _, move in ipairs(data) do
      if move.from == player then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerHand then
            if #move.moveInfo == 1 then
              local suit = Fk:getCardById(info.cardId):getSuitString(true)
              if suit == "log_nosuit" then
                table.insert(mark, "×")
              else
                table.insert(mark, suit)
              end
            else
              table.insert(mark, "×")
              break
            end
          end
        end
      end
    end
    if #mark > 4 then
      local n = #mark - 4
      for i = 1, n, 1 do
        table.remove(mark, 1)
      end
    end
    room:setPlayerMark(player, "@sk__shenfu", mark)
    player:drawCards(4 - player:getHandcardNum(), sk__shenfu.name)
    if not table.contains(mark, "×") and #mark == 4 and
        (mark[1] ~= mark[2] and mark[1] ~= mark[3] and mark[1] ~= mark[4] and
          mark[2] ~= mark[3] and mark[2] ~= mark[4] and mark[3] ~= mark[4]) then
      local to = room:askForChoosePlayers(player, table.map(room.alive_players, Util.IdMapper), 1, 1,
        "#sk__shenfu-choose", sk__shenfu.name, true)
      if #to > 0 then
        room:damage({
          from = player,
          to = room:getPlayerById(to[1]),
          damage = 1,
          damageType = fk.ThunderDamage,
          skillName = sk__shenfu.name,
        })
      end
    end
  end,
})

sk__shenfu:addLoseEffect(function(self, player, is_death)
  player.room:setPlayerMark(player, "@sk__shenfu", 0)
end)

sk__shenfu:addEffect(fk.Deathed, {
  can_refresh = function(self, event, target, player, data)
    if target == player then
      return player:hasSkill("sk__shenfu", true, true)
    end
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "@sk__shenfu", 0)
  end,
})

return sk__shenfu
